//
//  TJMInePresent.m
//  BaoDian
//
//  Created by mac on 2024/8/6.
//

#import "TJMinePresent.h"
#import "TJUserModel.h"
#import "TJStaffManageModel.h"
#import "TJStaffListModel.h"
#import "TJActiveCodeModel.h"
#import "TJTaskManageModel.h"
#import "TJTaskModel.h"
#import "TJClockInRecardModel.h"


@implementation TJMinePresent


+ (void)loginWithAccount:(NSString *)account password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/login/app" parameters:@{
        @"mobile": account,
        @"password": password.sha1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        TJUserInfo.account = account;
        TJUserInfo.token = data[@"token"];
        TJUserInfo.isMerchant = [data[@"userType"] intValue] == 2;
        TJUserInfo.isLogin = YES;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:loginSucceedNotification object:nil];
        
        [self getUserInfoComplete:nil];
    } failure:^(NSError * _Nonnull error) {
        
        !complete ?: complete(error);
    }];
}

+ (void)getUserInfoComplete:(void (^__nullable)(TJUserModel *userModel, NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork GET:@"/user/user/userInfo" parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        TJUserModel *model = [TJUserModel modelWithUserInfo:data];
        TJUserInfo.isMerchant = model.userType == 2;
        [model bg_saveOrUpdateAsync:nil];
        
        !complete ?: complete(model, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)laodStaffManagerListComplete:(void (^__nullable)(NSArray *datas, NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork GET:@"/user/userGroup/page" parameters:@{
        @"pageNum": @1,
        @"pageSize": @60
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *datas = [TJStaffManageModel mj_objectArrayWithKeyValuesArray:data];
        !complete ?: complete(datas, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)laodStaffListWithPageIndex:(NSInteger)pageIndex groupId:(NSString *)groupId complete:(void (^__nullable)(NSArray *datas, NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork GET:@"/user/appUser/staff" parameters:@{
        @"pageNum": @(pageIndex),
        @"pageSize": @60,
        @"groupId": groupId
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSArray *datas = [TJStaffListModel mj_objectArrayWithKeyValuesArray:data];
        
        !complete ?: complete(datas, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)createStaffWithGroupId:(NSString *)groupId account:(NSString *)account password:(NSString *)password realName:(NSString *)realName complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/createStaff" parameters:@{
        @"groupId": groupId,
        @"realName": realName,
        @"mobile": account,
        @"password": password.sha1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)deleteStaffWithUserId:(NSString *)userId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POST:@"/user/appUser/userDelete" parameters:@{
        @"userId": userId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)disableStaffWithUserId:(NSString *)userId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POST:@"/user/appUser/userDisable" parameters:@{
        @"userId": userId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)enableStaffWithUserId:(NSString *)userId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POST:@"/user/appUser/userEnable" parameters:@{
        @"userId": userId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)douYinLicenseWithCode:(NSString *)code complete:(void (^__nullable)(TJUserModel * _Nullable model, NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/staffBindDy" parameters:@{
        @"code": code,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        dispatch_async_sql_queue(^{
            TJUserModel *model = [TJUserModel currentAccountModel];
            TJUserInfo.isMerchant = model.userType == 2;
            [model bg_saveOrUpdate];
            
            dispatch_async_main_queue(^{
                !complete ?: complete(model, nil);
            });
        });
        
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}


+ (void)setPOI:(NSString *)poi complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/updatePoi" parameters:@{
        @"poiInfo": poi,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)setPOI:(NSString *)poi clockPlanId:(NSString * _Nullable)clockPlanId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    if (isEmptyString(clockPlanId)) {
        [self setPOI:poi complete:complete];
        return;
    }
    
    [TJNetworking.shareNetwork POSTBodyJSON:@"/escrow/clockPlan/updatePoi" parameters:@{
        @"poiInfo": poi,
        @"clockPlanId": clockPlanId
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)createStaffGroupWithActiveCode:(NSString *)activeCode name:(NSString *)name supplierId:(NSString *)supplierId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/userGroup/add" parameters:@{
        @"activeCode": activeCode,
        @"name": name,
        @"supplierId": supplierId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)deleteStaffGroupWithGroupId:(NSString *)groupId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POST:@"/user/userGroup/delete" parameters:@{
        @"groupId": groupId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)modifyStaffGroupWithGroupId:(NSString *)groupId name:(NSString *)name complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/userGroup/updateName" parameters:@{
        @"id": groupId,
        @"name": name,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loadActiveCodeListWithPageIndex:(NSInteger)pageIndex isUse:(BOOL)isUse complete:(void (^__nullable)(NSArray *datas, NSError * _Nullable error))complete {
    
    NSString *url = isUse ? @"/user/app/activation/usedPage" : @"/user/app/activation/page";
    
    [TJNetworking.shareNetwork GET:url parameters:@{
        @"pageNum": @1,
        @"pageSize": @60,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *datas = [TJActiveCodeModel mj_objectArrayWithKeyValuesArray:data];
        !complete ?: complete(datas, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)loadTaskListWithPageIndex:(NSInteger)pageIndex complete:(void (^__nullable)(NSArray *datas, NSError * _Nullable error))complete {

    NSString *url = TJUserInfo.isMerchant ? @"/escrow/taskStore/supplierPage" : @"/escrow/taskStore/staffPage";
    [TJNetworking.shareNetwork GET:url parameters:@{
        @"pageNum": @1,
        @"pageSize": @60,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *datas = [TJTaskManageModel mj_objectArrayWithKeyValuesArray:data];
        !complete ?: complete(datas, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)loadTaskDetailWithTaskId:(NSString *)taskId complete:(void (^__nullable)(NSArray *datas, NSString *remainTime,  NSError * _Nullable error))complete {
    
    BOOL isMerchant = TJUserInfo.isMerchant;
    
    NSString *url = isMerchant ? @"/escrow/taskStore/detail" : @"/escrow/taskStore/detailForStaff";
    
    [TJNetworking.shareNetwork GET:url parameters:@{
        @"taskId": taskId,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *datas = [TJTaskModel mj_objectArrayWithKeyValuesArray:data[@"clockPlanList"]];
        !complete ?: complete(datas, data[@"remainTime"], nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, @"0", error);
    }];
}

+ (void)checkPoi:(NSString *)poi complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/checkPoi" parameters:@{
        @"poiInfo": poi,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)checkPoi:(NSString *)poi clockPlanId:(NSString *)clockPlanId complete:(void (^__nullable)(NSError * _Nullable error))complete {

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:poi forKey: @"poiInfo"];
    [parameters setValue:clockPlanId forKey:@"clockPlanId"];

    [TJNetworking.shareNetwork POSTBodyJSON:@"/escrow/clockPlan/checkPoi" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}



+ (void)modifyName:(NSString *)name complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/updateUserRealName" parameters:@{
        @"realName": name,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loadClockInRecardListWithPageIndex:(NSInteger)pageIndex complete:(void (^__nullable)(NSArray *datas, NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork GET:@"/escrow/clockPlan/app/staffHistoryPage" parameters:@{
        @"pageNum": @1,
        @"pageSize": @60,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *datas = [TJClockInRecardModel mj_objectArrayWithKeyValuesArray:data];
        !complete ?: complete(datas, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}


+ (void)updateClockPlanWithId:(NSString *)Id status:(NSString *)status complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/escrow/clockPlan/updateStatus" parameters:@{
        @"id": Id,
        @"status": status
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)getClockPlanShareIdWithId:(NSString *)Id complete:(void (^__nullable)(NSString *shareId, NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POST:@"/escrow/clockPlan/toClockPlan" parameters:@{
        @"id": Id,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"shareId"], nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)modifyPassword:(NSString *)password mobile:(NSString *)mobile code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/updatePassword" parameters:@{
        @"code": code,
        @"mobile": mobile,
        @"newPassword": password,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)forgetPassword:(NSString *)password mobile:(NSString *)mobile code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork POSTBodyJSON:@"/user/appUser/retrievePassword" parameters:@{
        @"code": code,
        @"mobile": mobile,
        @"newPassword": password,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)sendCodeWithMobile:(NSString *)mobile complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [TJNetworking.shareNetwork GET:@"/escrow/common/sendCaptchaNoLogin" parameters:@{
        @"mobile": mobile,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

@end


